/**
 * 
 */
package com.jie.eclipse.pan.gef.listener;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.jface.util.TransferDragSourceListener;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;

import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.gef.dialog.BaseDialog;
import com.jie.eclipse.pan.gef.dialog.TextDialog;
import com.jie.eclipse.pan.gef.entry.PanPaletteTemplateEntry;
import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.utils.Utils;
import com.jie.eclipse.pan.views.PanDeveloperhelperView;


/**
 * @author yanjie
 *
 */
public class PanHelperTransferDragSourceListener implements TransferDragSourceListener{
	private Transfer ts = null ;
	private PanDeveloperhelperView developerhelperView = null;
	
	/**
	 * 
	 */
	public PanHelperTransferDragSourceListener(PanDeveloperhelperView viewPart,Transfer ts) {
		this.developerhelperView = viewPart;
		this.ts = ts;
	}
	
	public void dragFinished(DragSourceEvent event) {
		
	}

	public void dragSetData(DragSourceEvent event) {
		PaletteEntry entry = this.developerhelperView.getSelectEntry();
		if(entry != null){
			BaseDataEntity row = null;
			if(entry instanceof PanPaletteTemplateEntry){
				row = (BaseDataEntity)((PanPaletteTemplateEntry)entry).getTemplate();
			}if(entry instanceof CombinedTemplateCreationEntry){
				row = (BaseDataEntity)((CombinedTemplateCreationEntry)entry).getToolProperty("data");
			}
			if(row != null){
				if("tag".equals(row.gpsv("type"))){
					BaseDialog dialog = getDialog(row);
		            if (dialog != null && dialog.open() == BaseDialog.OK) {  
		            	event.data = row.gpsv("code").replace("&lt;", "<");  
		            }else{
		            	event.data=new String(" ");
		            }
				}else{
	            	event.data = row.gpsv("codetp").replace("&lt;", "<");
	            }
			}
		}
	}

	public void dragStart(DragSourceEvent arg0) {
		//Utils.print("dragStart");
	}

	public Transfer getTransfer() {
		return this.ts;
	}
	
	public BaseDialog getDialog(BaseDataEntity row){
		String key = row.gpsv("sn");
		String strClass = row.gpsv("codetp");
		
		BaseDialog dialog = null;
		if(!StringHelper.IsNullOrEmpty(strClass)){
			dialog = newInstance(strClass, new Object[]{Display.getDefault().getActiveShell(),row,"default"});
		}
		return dialog;
	}
	
	public BaseDialog newInstance(String className, Object[] args)  {
		try {
			Class newoneClass = Class.forName(className);
			Class[] argsClass = new Class[args.length];
			for (int i = 0, j = args.length; i < j; i++) {
				argsClass[i] = args[i].getClass();
			}
			
			Constructor cons = newoneClass.getConstructor(argsClass);
			return (BaseDialog)cons.newInstance(args);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
